#include "matrixcalculatewindow.h"
#include "ui_matrixcalculatewindow.h"

MatrixCalculateWindow::MatrixCalculateWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MatrixCalculateWindow)
{
    ui->setupUi(this);
    //初始化成员变量
    this->btn_calculator = ui->btn_calculator;
    this->btn_complex = ui->btn_complex;
    this->btn_set = ui->btn_set;
    this->textEdit_m1 = ui->textEdit_m1;
    this->textEdit_m2 = ui->textEdit_m2;

    this->textBrowser_result = ui->textBrowser_result;
    this->rbtn_add = ui->rbtn_add;
    this->rbtn_sub = ui->rbtn_sub;
    this->rbtn_mul = ui->rbtn_sub;
    this->btn_clear = ui->btn_clear;
    this->btn_get = ui->btn_get;

    /* 绑定单击事件 */
    connect(btn_calculator, &QPushButton::clicked, [=]{
        this->hide();
        ApplicationManager::windows[0]->setGeometry(this->geometry());
        ApplicationManager::windows[0]->show();
    });
    connect(btn_complex, &QPushButton::clicked, [=]{
        this->hide();
        ApplicationManager::windows[1]->setGeometry(this->geometry());
        ApplicationManager::windows[1]->show();
    });
    connect(btn_set, &QPushButton::clicked, [=]{
        this->hide();
        ApplicationManager::windows[3]->setGeometry(this->geometry());
        ApplicationManager::windows[3]->show();
    });
    connect(btn_clear, &QPushButton::clicked, [=]{
        textEdit_m1->clear();
        textEdit_m2->clear();
        textBrowser_result->setHtml("<p style='font-size:20px'><b>结果展示在这...</b></p>");
    });

    connect(btn_get, &QPushButton::clicked, [=]{
        bool error = false;
        QString text1 = textEdit_m1->toPlainText();
        QString text2 = textEdit_m2->toPlainText();
        if(text1 == "" || text2 == ""){
            textBrowser_result->setHtml("<p style='font-size:20px;color:red'><b>请输入数据</b></p>");
            return;
        }
        QStringList list1 = text1.trimmed().split(QRegExp("[\r\n]"),QString::SkipEmptyParts);
        QStringList list2 = text2.trimmed().split(QRegExp("[\r\n]"),QString::SkipEmptyParts);
        int m1 = list1.size(), n1 = list1[0].trimmed().split(" ").length();
        int m2 = list2.size(), n2 = list2[0].trimmed().split(" ").length();
        int nums1[m1][MAX_LEN], nums2[m2][MAX_LEN];

        //第一个矩阵数据
        int curRow = 0;
        for(QString s : list1){
            //行向量
            QStringList list = s.trimmed().split(" ");
            if(list.size() != n1){
                error = true;
                break;
            }
            for(int i = 0; i < n1; i++){
                //输入为整数
                if(Util::isIntNumStr(list[i])){
                    nums1[curRow][i] = list[i].toInt();
                }else{
                    error = true;
                    break;
                }
            }
            curRow++;
        }
        if(error){
            textBrowser_result->setHtml("<p style='font-size:20px;color:red'><b>ERROR</b></p>");
            return;
        }
        //第二个矩阵数据
        curRow = 0;
        for(QString s : list2){
            //行向量
            QStringList list = s.trimmed().split(" ");
            if(list.size() != n2){
                error = true;
                break;
            }
            for(int i = 0; i < n2; i++){
                if(Util::isIntNumStr(list[i])){
                    nums2[curRow][i] = list[i].toInt();
                }else{
                    //输入为整数
                    error = true;
                    break;
                }
            }
            curRow++;
        }
        if(error){
            textBrowser_result->setHtml("<p style='font-size:20px;color:red'><b>Input ERROR</b></p>");
            return;
        }
        Matrix matrix1(m1, n1, nums1);
        Matrix matrix2(m2, n2, nums2);
//        qDebug()<<matrix1.toString()<<endl;
//        qDebug()<<matrix2.toString()<<endl;
        if(rbtn_add->isChecked()){
            if(m1 != m2 || n1 != n2){
                textBrowser_result->setHtml("<p style='font-size:20px;color:red'><b>矩阵加法两矩阵行数和列数要对应相同</b></p>");
                return;
            }
            matrix = service.add(matrix1, matrix2);
        }else if(rbtn_sub->isChecked()){
            if(m1 != m2 || n1 != n2){
                textBrowser_result->setHtml("<p style='font-size:20px;color:red'><b>矩阵减法两矩阵行数和列数要对应相同</b></p>");
                return;
            }
            matrix = service.sub(matrix1, matrix2);
        }else{
            if(n1 != m2){
                textBrowser_result->setHtml("<p style='font-size:20px;color:red'><b>矩阵乘法第一个矩阵的列数要等于第二个矩阵的行数</b></p>");
                return;
            }
            matrix = service.mul(matrix1, matrix2);
        }
        textBrowser_result->setHtml(matrix.toString());
    });
}

MatrixCalculateWindow::~MatrixCalculateWindow()
{
    delete ui;
}
